#include <myheader_c_plus.h>
using std::endl;
using std::cout;

class Base{
public:
    //无参构造
    Base()
        :_pbase(new char[1]())
    {
        cout << "Base()" << endl;

    }

    //构造函数
    Base(const char *pbase)
        :_pbase(new char[strlen(pbase) + 1]())
    {
        cout << "Base(const char *)" << endl;

    }

    //拷贝构造函数
    Base(const Base & rhs)
        :_pbase(new char[strlen(rhs._pbase) + 1]())
    {
        strcpy(_pbase, rhs._pbase);
    }

    //析构函数
    virtual ~Base(){
        cout << "~Base()" << endl;
        if(_pbase){
            delete [] _pbase;
            _pbase = nullptr;
        }
    }


private:
    char *_pbase;

};


class Drived : public Base{
public:
    //无参构造函数
    Drived()
        :Base()
         ,_pdrived(new char[1]())
    {
        cout << "Drived()" << endl;

    }

    //构造函数
    Drived(const char * pbase, const char * pdrived)
        :Base(pbase)
         ,_pdrived(new char[strlen(pdrived) + 1]())
    {
        strcpy(_pdrived, pdrived);
        cout << "Drived(const char *, int)" << endl;

    }

    //析构函数
    ~Drived(){
        if(_pdrived){
            delete [] _pdrived;
            _pdrived = nullptr;
        }
        cout << "~Drived" << endl;
    }

private:
    char * _pdrived;
};
int main(int argc, char *argv[])
{
    //在堆上申请一个对象,并且使用多态，必须要让析构函数变成虚函数
    Base * pbase = new Drived("hello","nihao");
    delete pbase;  
    return 0;
}

